package com.eqxiu.ieditor.api.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/sentinel")
public class TestController {
	
	@GetMapping("/flow")
	public String test() {
		return "111111122222222333333344444444";
	}


	@GetMapping(value = "/degrade")
	@SentinelResource(value = "degrade", blockHandler = "degradeBlockHandler")
	public String degrade() {
		Random random = new Random();
		double v = random.nextDouble();
		if (v > 0.5d) {
			throw new RuntimeException("========RuntimeException=========");
		} else {
			return "degrade return!!!!!!!!!!!!!!!!!!";
		}
	}

	public String degradeBlockHandler(BlockException exception) {
		System.out.println("P2222222222222222@@@degradeBlockException@@@@@@@@@@");
		return "P2222222222222222@@@degradeBlockException@@@@@@@@@@";
	}

	@GetMapping(value = {"/param"})
	@SentinelResource(value = "param", blockHandler = "ppppBlockHandler")
	public String param(@RequestParam String id, @RequestParam String name) {
		return "11111111111111111111 return!!id="+id + ", name="+name;
	}

	public String ppppBlockHandler(BlockException exception) {
		System.out.println("11111111111111111111@@@@@paramBlockException@@@@@@@@");
		return "11111111111111111111@@@@@paramBlockException@@@@@@@@";
	}

}
